#include ".\dxMgr.h"
#include ".\dxGameMgr.h"
#include ".\dxUtility.h"
#include ".\dxText.h"
#include ".\dxPlane.h"
#include ".\dxModel.h"
#include ".\dxLight.h"
#include ".\dxCamera.h"
#include ".\dxTimer.h"
#include ".\dxAnimation.h"
#include ".\dxInput.h"
#include ".\dxSound.h"
#include ".\dxMenu.h"
#include ".\dxCustomVertex.h"
#include ".\dxWorldTransformation.h"
#include ".\Common\dxTypes.h"
#include ".\time.h"

static bool startGame;
static bool rotCam;


void sleep( clock_t wait )
{
   clock_t goal;
   goal = wait + clock();
   while( goal > clock() );
}

dxGameMgr::dxGameMgr(void)
{
	startGame = false;
	rotCam = false;
	m_bSleep = FALSE;
}

dxGameMgr::~dxGameMgr(void)
{
	if( dxManager != NULL) 
	{
        delete dxManager;
		dxManager = NULL;
	}
	if( textManager != NULL) 
	{
        delete textManager;
		textManager = NULL;
	}
}

bool dxGameMgr::init(HWND hWnd, HINSTANCE hInst) 
{
	dxManager = new dxMgr();
	if(FULLSCREEN == 0)
		dxManager->Initialise(hWnd, hInst, SCREEN_WIDTH, SCREEN_HEIGHT, true);
	else
		dxManager->Initialise(hWnd, hInst, SCREEN_WIDTH, SCREEN_HEIGHT, false);
	if (!dxManager)
	{
		return false;
	}
	
	textManager = new dxText();
	textManager->init(32,dxManager->getD3DDevice());
	if (!textManager)
	{
		return false;
	}

	input = new dxInput();
	input->init(hInst, hWnd);

	menu = new dxMenu();
	menu->init(input, dxManager->getD3DDevice());
	active = MENU;

	/*cstVertex = new dxCustomVertex();
	cstVertex->initVertex(dxManager->getD3DDevice());*/

	testSurface = new dxPlane();
	testSurface->loadPlane(dxManager->getD3DDevice(),"avion1.png", false);
	if (!testSurface)
	{
		return false;
	}
	
	/*camera = new dxCamera();
	camera->createCamera(dxManager->getD3DDevice(), 1.0f, 1000.f);
	camera->setLookAt(D3DXVECTOR3(0.0f,0.0f,0.0f));
	camera->setPosition(D3DXVECTOR3(0.0f,0.0f,20.0f));

	lights = new dxLight(dxManager->getD3DDevice());
	int light_custom = lights->createLightCustom();
	int light_one = lights->createLight();
	int light_two = lights->createLight();
	int light_three = lights->createLight();
	lights->setPosition(light_two, D3DXVECTOR3(25.0f,0.0f,5.0f));
	lights->setPosition(light_three, D3DXVECTOR3(-25.0f,0.0f,5.0f));*/
	//lights->setSpecular(light_two,255,150,100);

	//create a new animation test object
	animTest = new dxAnimation();
	animTest->init(dxManager->getD3DDevice());
	animTest->setPosition(D3DXVECTOR3(0.0f,30.0f,0.0f));

	/*space = new dxAnimation();
	space->initSpaceship(dxManager->getD3DDevice());
	space->setPosition(D3DXVECTOR3(5.0f,0.0f,0.0f));*/

	sounds = new dxSound();
	sounds->init(hWnd);
	soundOne = sounds->loadFile(CUtility::GetTheCurrentDirectory() + "/data/sunetMeniu.wav");
	soundTwo = sounds->loadFile(CUtility::GetTheCurrentDirectory() + "/data/ding.wav");
	sunetCurent=soundOne;

	soundContinue = sounds->loadFile(CUtility::GetTheCurrentDirectory() + "/data/s1.wav");
	soundNewGame = sounds->loadFile(CUtility::GetTheCurrentDirectory() + "/data/s2.wav");
	soundExit = sounds->loadFile(CUtility::GetTheCurrentDirectory() + "/data/tvupss01.wav");
	
	soundG1 = sounds->loadFile(CUtility::GetTheCurrentDirectory() + "/data/Gear1.wav");
	soundG1G2 = sounds->loadFile(CUtility::GetTheCurrentDirectory() + "/data/Gear1ToGear2.wav");
	soundG2 = sounds->loadFile(CUtility::GetTheCurrentDirectory() + "/data/Gear2.wav");
	soundG2G3 = sounds->loadFile(CUtility::GetTheCurrentDirectory() + "/data/Gear2ToGear3.wav");

	

	sounds->Mp3Init();
	//sounds->Mp3Load("muzica.mp3");

	timer = new dxTimer();
	timer->init(60);

	return true;
}

void dxGameMgr::update(void)
{
	input->getInput();
	/*int framesToUpdate;
	framesToUpdate = timer->framesToUpdate();
	char textOut[60] = "";
	sprintf_s(textOut,"X, and Y Mouse Movement = , x %02d, y %02d ", input->getMouseMovingX(), input->getMouseMovingY());
	*/

	if (dxManager->Loader)
	{
		if (input->keyDown(DIK_LEFT))
		{
			MoveShipXL();	
		}
		else if (input->keyDown(DIK_RIGHT))
		{	
			MoveShipXR();	
		}
		
		if (input->keyDown(DIK_UP))
		{
			MoveShipZR();
			switch (dxManager->speedState)
			{
				case 1 :
					sunetCurent=soundG1;
					break;
				case 2 :
					sunetCurent=soundG1G2;
					break;
				case 3 :
					sunetCurent=soundG2;
					break;
			}
			sounds->playSound(sunetCurent);
		}
		else if (input->keyUp(DIK_UP))
		{
			switch (dxManager->speedState)
			{
				case 1 : 
					sounds->stopSound(sunetCurent);
					break;
				case 2 :
					sounds->stopSound(sunetCurent);
					break;
				case 3 :
					sounds->stopSound(sunetCurent);
					break;
			}
			MoveShipZR2();
		}

		if (input->keyDown(DIK_DOWN))
		{	
			MoveShipZL();
		}
		else if (input->keyDown(DIK_Q))
		{
			MoveUpUp();	
		}
		else if (input->keyDown(DIK_W))
		{
			MoveUpDown();	
		}
		else if (input->keyDown(DIK_E))
		{
			YawEntityL();
		}
		else if (input->keyDown(DIK_R))
		{
			YawEntityR();
		}
		else if (input->keyDown(DIK_T))
		{
			PitchEntityL();
		}
		else if (input->keyDown(DIK_Y))
		{
			PitchEntityR();
		}
		else if (input->keyDown(DIK_U))
		{
			RollEntityL();
		}
		else if (input->keyDown(DIK_I))
		{
			RollEntityR();
		}
		else if (input->isButtonDown(0))
		{
			MoveForwardUp();
		}
		else if (input->isButtonDown(1))
		{
			MoveForwardDown();
		}
		else if (input->isButtonDown(2))
		{
			MoveUpUp();
		}
		else if (input->keyDown(DIK_Z))
		{
			MoveShipXL();	
		}
		else if (input->keyDown(DIK_X))
		{
			MoveShipXR();	
		}
			else if (input->keyDown(DIK_C))
		{
			MoveShipYL();	
		}
		else if (input->keyDown(DIK_V))
		{
			MoveShipYR();	
		}
		else if (input->keyDown(DIK_B))
		{
			MoveShipZL();
		}
		else if (input->keyDown(DIK_N))
		{
			MoveShipZR();
		}
		else if (input->keyDown(DIK_A))
		{
			MoveShipRotateX();
		}
		else if (input->keyDown(DIK_S))
		{
			MoveShipRotateX_();
		}
		else if (input->keyDown(DIK_D))
		{
			MoveShipRotateY();
		}
		else if (input->keyDown(DIK_F))
		{
			MoveShipRotateY_();
		}
		else if (input->keyDown(DIK_G))
		{
			MoveShipRotateZ();
		}
		else if (input->keyDown(DIK_H))
		{
			MoveShipRotateZ_();
		}
		else if (input->keyDown(DIK_M))
		{
			dxManager->setAnimation(1);
		}
		else if (input->keyDown(DIK_P))
		{
			dxManager->wireFrameMode();
		}
	}

	//MENU
	if (active == MENU)
	{	
		
		sounds->Mp3Stop();
		menu->update();
		if (menu->getMessage() == dxMenu::EXIT)
		{	
			sounds->playSound(soundExit);
			sleep( (clock_t)2* CLOCKS_PER_SEC );
			active = NOTHING;
			delete menu;
			PostQuitMessage(0);	 
		}
		if (menu->getMessage() == dxMenu::NEW_GAME)
		{	
			sounds->playSound(soundNewGame);
			sleep( (clock_t)2* CLOCKS_PER_SEC );
			active=GAME;
		}
		if (menu->getMessage() == dxMenu::CONTINUE)
		{	
			sounds->playSound(soundContinue);
			sleep( (clock_t)2* CLOCKS_PER_SEC );
			active=GAME;
			dxManager->boolMenu =  false;
		}
	}

	if (active == GAME)
	{
		if(input->keyDown(DIK_ESCAPE))
		{
			menu = new dxMenu();
			menu->init(input, dxManager->getD3DDevice());
			active = MENU;
			//dxManager->boolMenu =  true;
		}
	}
	
	if (input->keyDown(DIK_RETURN))
	{
		startGame = true;
	}
	
	if (startGame)
	{
		dxManager->beginRender();
	}	
	
	if (active == MENU)
	{
		menu->render(dxManager->getD3DDevice());
	}
	
	std::string arrayPos[5] = {"130.0", "130.1", "130.2"};
	std::string posModel = ToString(dxManager->modelPosition3.z);
	if(posModel.substr(0,5) == arrayPos[0] || posModel.substr(0,5) == arrayPos[1] || posModel.substr(0,5) == arrayPos[2])
	{	
		dxManager->turnCar3 = true;
	}
	
	std::string arrayPos3[10] = {"601.0", "601.1", "601.2", "601.3", "601.4", "601.5", "601.6", "601.7", "601.8", "601.9"};
	std::string posModel3 = ToString(dxManager->modelPosition3.z);
	if(posModel3.substr(0,5) == arrayPos3[0] || posModel3.substr(0,5) == arrayPos3[1] || posModel3.substr(0,5) == arrayPos3[2] 
	|| posModel3.substr(0,5) == arrayPos3[3] || posModel3.substr(0,5) == arrayPos3[4] || posModel3.substr(0,5) == arrayPos3[5] 
	|| posModel3.substr(0,5) == arrayPos3[6] || posModel3.substr(0,5) == arrayPos3[7] || posModel3.substr(0,5) == arrayPos3[8] || posModel3.substr(0,5) == arrayPos3[9])
	{	
		if(!menu->menuWon)
		{
			menu->menuLose = true;
			menu->render(dxManager->getD3DDevice());
		}
	}
	
	std::string arrayPos2[10] = {"601.0", "601.1", "601.2", "601.3", "601.4", "601.5", "601.6", "601.7", "601.8", "601.9"};
	std::string posModel2 = ToString(dxManager->modelPosition2.z);
	if(posModel2.substr(0,5) == arrayPos2[0] || posModel2.substr(0,5) == arrayPos2[1] || posModel2.substr(0,5) == arrayPos2[2] 
	|| posModel2.substr(0,5) == arrayPos2[3] || posModel2.substr(0,5) == arrayPos2[4] || posModel2.substr(0,5) == arrayPos2[5] 
	|| posModel2.substr(0,5) == arrayPos2[6] || posModel2.substr(0,5) == arrayPos2[7] || posModel2.substr(0,5) == arrayPos2[8] || posModel2.substr(0,5) == arrayPos2[9])
	{	
		menu->menuWon = true;
		menu->render(dxManager->getD3DDevice());
	}
	
	if (dxManager->Loader)
	{
		std::string arrayPosCam[5] = {"150", "151", "153", "154", "155"};
		std::string posModel3 = ToString(dxManager->modelPosition3.z);
		if(posModel3.substr(0,3) == arrayPosCam[0] || posModel3.substr(0,3) == arrayPosCam[1] || posModel3.substr(0,3) == arrayPosCam[2] ||
		posModel3.substr(0,3) == arrayPosCam[3] || posModel3.substr(0,3) == arrayPosCam[4])
		{	
			rotCam = true;
		}
		if(!rotCam)
			dxManager->YawEntity(0.0004f);
	}	
	
	if(menu->menuLose == true || menu->menuWon == true)
	{
		menu->render(dxManager->getD3DDevice());
	}
	
	
	//textManager->drawText(textOut, 30, 30, 300, 300);
	//cstVertex->drawGraph(dxManager->getD3DDevice());
	//dxManager->drawGraph();
	//testSurface->render(dxManager->getD3DDevice());
	//animTest->render(dxManager->getD3DDevice());
	//space->render(dxManager->getD3DDevice());

	dxManager->endRender();

}

void dxGameMgr::ToggleControlsDisplay()
{
	dxManager->ToggleControlsDisplay();
}

void dxGameMgr::loadFileX(const std::string &filename)
{
	dxManager->LoadXFile(filename,1);
}

void dxGameMgr::NextAnimationX()
{
	dxManager->NextAnimation();
}

void dxGameMgr::MoveForwardUp()
{
	dxManager->MoveForwardUp();
}

void dxGameMgr::MoveForwardDown()
{
	dxManager->MoveForwardDown();
}

void dxGameMgr::MoveRight()
{
	dxManager->MoveRight();
}

void dxGameMgr::MoveLeft()
{
	dxManager->MoveLeft();
}

void dxGameMgr::MoveUpUp()
{
	dxManager->MoveUpUp();
}

void dxGameMgr::MoveUpDown()
{
	dxManager->MoveUpDown();
}

void dxGameMgr::YawEntityR()
{
	dxManager->YawEntity(-cameraYawRollPitch);
}

void dxGameMgr::YawEntityL()
{
	dxManager->YawEntity(cameraYawRollPitch);	
}

void dxGameMgr::PitchEntityL()
{
	dxManager->PitchEntity(-cameraYawRollPitch);
}

void dxGameMgr::PitchEntityR()
{
	dxManager->PitchEntity(cameraYawRollPitch);
}
		
void dxGameMgr::RollEntityL()
{
	dxManager->RollEntity(-cameraYawRollPitch);
}

void dxGameMgr::RollEntityR()
{
	dxManager->RollEntity(cameraYawRollPitch);
}

void dxGameMgr::MoveMouseLeft(float value)
{
	dxManager->MoveForwardUp();
}

void dxGameMgr::MoveMouseRight(float value)
{
	dxManager->MoveForwardDown();
}

void dxGameMgr::MoveShipXL()
{
	dxManager->MoveShipXL();
}

void dxGameMgr::MoveShipXR()
{
	dxManager->MoveShipXR();
}

void dxGameMgr::MoveShipYL()
{
	dxManager->MoveShipYU();
}

void dxGameMgr::MoveShipYR()
{
	dxManager->MoveShipYD();
}

void dxGameMgr::MoveShipZL()
{
	dxManager->MoveShipZF();
	
}

void dxGameMgr::MoveShipZR()
{
	dxManager->MoveShipZB();
}

void dxGameMgr::MoveShipZR2()
{
	dxManager->MoveShipZB2();
}

void dxGameMgr::MoveShipRotateX_()
{
	dxManager->YawEntity3(-cameraYawRollPitch);
}

void dxGameMgr::MoveShipRotateX()
{
	dxManager->YawEntity3(cameraYawRollPitch);
}

void dxGameMgr::MoveShipRotateY_()
{
	dxManager->PitchEntity3(-cameraYawRollPitch);
}

void dxGameMgr::MoveShipRotateY()
{
	dxManager->PitchEntity3(cameraYawRollPitch);
}

void dxGameMgr::MoveShipRotateZ_()
{
	dxManager->RollEntity3(-cameraYawRollPitch);
}

void dxGameMgr::MoveShipRotateZ()
{
	dxManager->RollEntity3(cameraYawRollPitch);
}
